<html>
  <head>
  <title>Disc Golf Scores</title>
  </head>
  <body>

<?php include 'round.php'; ?>

<table border="1">
  <tr>
    <td>Hole</td>

<?php

    $roundId;

    $currentHole = 1;
    $round = new Round($_POST['course'], $_POST['holes'], $_POST['player1'], $_POST['player2'], $_POST['player3'], $_POST['player4']);
    
    // Hole number plus amount of results to input
      foreach ($round->getPlayerList() as $value) {
        echo '<td>' . $value . '</td>';
      }
    echo '</tr>'; // EKA RIVI
    
// Here should be player scores by hole!
  $results = $round->chunkResults($_POST);

// Here we have array of array of scores per hole (hole1_player1, hole1_player2) etc.

  foreach ($results as $temppi) {
    echo '<td>' . $currentHole . '</td>';
    foreach ($temppi as $pemppi) {
      echo '<td>' . $pemppi . '</td>';
    }  
    $currentHole = ++$currentHole;
    echo '</tr>'; 
  }
    echo '</tr>';
?>

  <tr>
    <td>Results:</td>

<?php
// Count scores and insert into table
  $round->countTotalScores($results, $round->getNumberOfPlayers());
  $scores = $round->getTotals();

  foreach ($scores as $res) {
    echo '<td>' . $res . '</td>';
  }

// ----------- END OF VIEW ---------------

// Connect to database
$db = mysql_connect('localhost', 'kattilam_test', 'test') or die ('Unable to connect to DB!!');
mysql_select_db('kattilam_discgolfmobile', $db) or die (mysql_error($db));

// Write round info to DB
$query = 'INSERT INTO ROUND (date, course) VALUES (Now( ), "' . $_POST['course'] .'")';
mysql_query($query, $db) or die (mysql_error($db));

/* Write result to db
 * 1. Fetch ID from round - DONE
 */
 
$query2 = 'SELECT MAX(id) FROM ROUND';
$result2 = mysql_query($query2, $db) or die (mysql_error($db));

while ($row = mysql_fetch_array($result2)) {
  extract($row);
  $roundId = $row[0];
  }

// TODO: Luuppi insert queryn sisaan
$playerCounter = 1;
foreach ($round->getPlayerList() as $value) {
  
  $playerResults = $round->getPlayerResults($playerCounter, $results);
  
  $query3 = 'INSERT INTO RESULT (round_id, player_name, score_1, score_2, score_3, score_4, score_5, score_6, score_7, score_8, score_9,
                                 score_10, score_11, score_12, score_13, score_14, score_15, score_16, score_17, score_18, total) 
                                 VALUES('. $roundId . ',"' . $value . '",' . $playerResults[0] .',' . $playerResults[1] .', 
                                       ' . $playerResults[2] .', ' . $playerResults[3] .', ' . $playerResults[4] .',
                                       ' . $playerResults[5] .', ' . $playerResults[6] .', ' . $playerResults[7] .',
                                       ' . $playerResults[8] .', ' . $playerResults[9] .', ' . $playerResults[10] .',
                                       ' . $playerResults[11] .', ' . $playerResults[12] .', ' . $playerResults[13] .',
                                       ' . $playerResults[14] .', ' . $playerResults[15] .', ' . $playerResults[16] .',
                                       ' . $playerResults[17] .',' . $scores[$playerCounter - 1] .')';
  
  $playerCounter = $playerCounter + 1;
  
  mysql_query($query3, $db) or die (mysql_error($db));
}

?>
  </table>

<a href="history.php">Previous rounds</a>
<a href="frontpage.php">New round</a>

 </body>
</html>
